//
//  MineAddressAddInfoTableViewCell.m
//  MJApp
//
//  Created by wangliang on 15/3/29.
//  Copyright (c) 2015年 wangliang. All rights reserved.
//

#import "MineAddressAddInfoTableViewCell.h"

#import "TextPickerView.h"
#import "WLJson.h"
@interface MineAddressAddInfoTableViewCell ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (nonatomic,strong) TextPickerView *addressPickerView;

@end

@implementation MineAddressAddInfoTableViewCell

+ (instancetype)initWithTableView:(UITableView *)tableView {
    
    static NSString *cellID = @"MineAddressAddInfoTableViewCell";
    MineAddressAddInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        
        cell =  WL_XIB(@"MineAddressAddInfoTableViewCell");
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.cellHigh = CGRectGetHeight(cell.frame);
    return cell;
    
}

- (void)setInfoDic:(NSDictionary *)infoDic {
    
    _infoDic = infoDic;
    
    NSInteger index = [infoDic[@"index"] integerValue];
    self.textField.tag = index;
        
    self.textField.placeholder = infoDic[@"desc"];
    self.textField.text = infoDic[@"value"];
    self.textField.userInteractionEnabled = YES;
    if (index == 1 || index == 2) {
        
        self.textField.keyboardType = UIKeyboardTypeNumberPad;
    }
    if (index == 3) {
        
        if ([infoDic[@"value"] isEqual:@""]) {
            
            self.textField.text = infoDic[@"desc"];
        }else {
            self.textField.text = infoDic[@"value"];
        }
        
        if (self.addressPickerView == nil) {
            
            self.addressPickerView = [[TextPickerView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), 200)];
            self.addressPickerView.pickerArray = infoDic[@"addrList"];
            /**
             
             数据格式为
             [{[{[{}],[]},{}],[]},{},{}]
             
             小----------------->大
             字典->数组->字典->数组->字典->数组
             */
//            NSMutableArray *oneArray = [[NSMutableArray alloc] init];
//            for (int i = 0; i < 10; i ++) {
//                
//                NSMutableDictionary *oneDic = [NSMutableDictionary dictionary];
//                NSMutableArray *twoArray = [[NSMutableArray alloc] init];
//                for (int j = 0; j < 10 ; j ++) {
//                    
//                    NSMutableDictionary *twoDic = [NSMutableDictionary dictionary];
//                    NSMutableArray *threeArray = [[NSMutableArray alloc] init];
//                    for (int k = 0; k < 10; k ++) {
//                        
//                        NSMutableDictionary *threeDic = [NSMutableDictionary dictionary];
//                        threeDic[@"id"] = [NSString stringWithFormat:@"%d-%d-%d",i,j,k];
//                        threeDic[@"name"] = [NSString stringWithFormat:@"%d-%d-%d",i,j,k];
//                        [threeArray addObject:threeDic];
//                    }
//                    twoDic[@"id"] = [NSString stringWithFormat:@"%d-%d",i,j];
//                    twoDic[@"name"] = threeArray;
//                    [twoArray addObject:twoDic];
//                }
//                oneDic[@"id"] = [NSString stringWithFormat:@"%d",i];
//                oneDic[@"name"] = twoArray;
//                [oneArray addObject:oneDic];
//            }
//
//            self.addressPickerView.pickerArray = oneArray;
        }
        self.textField.inputView = self.addressPickerView;
    }
    
}

- (void)awakeFromNib {
    
    self.textField.delegate = self;
}

- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string {
    
    NSString *allString = nil;
    if ([string isEqual:@""]) {
        
        //删除
        allString = [textField.text substringToIndex:textField.text.length - 1];
    }else {
        //添加
        if (range.length == 0) {
            
            allString = [NSString stringWithFormat:@"%@%@",textField.text,string];
        }else {
            
            allString = textField.text;
        }
        
    }
    [self callBack:allString];
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    
    if (textField.tag == 3) {
        
        textField.text = [self.addressPickerView selectedOfPickerView];
        [self callBack:textField.text];
    }
}

- (void)callBack:(NSString *)str {
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:self.infoDic];
    [dic setObject:str forKey:@"newString"];
    if ([self.delegate respondsToSelector:@selector(homeCell:withInfo:)]) {
        
        [self.delegate homeCell:self withInfo:dic];
    }
    
}



- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
